package com.xin.components.base
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.system.IME;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * 单行输入纯文本
	 * @author daichangxin
	 * 
	 */	
	public class InputTextBase extends LiteSprite
	{
		protected var _tf:TextField;
		protected var _password:Boolean = false;
		protected var _text:String;
		protected var _format:TextFormat;
		protected var _blNumber:Boolean = false;
		protected var _inputDes:String = null;
		/**
		 * 数字类型输入时的最大值
		 */		
		protected var _max:int;
		/**
		 * 数字类型输入时的最小值
		 */		
		protected var _min:int;
		
		public function InputTextBase(w:int, h:int, text:String = "", changeHandler:Function = null, inputDes:String = null)
		{
			_text = text;
			_inputDes = inputDes;
			
			if (changeHandler != null)
			{
				addEventListener(Event.CHANGE, changeHandler);
			}
			
			super(w, h);
		}
		
		protected override function addChildren():void
		{
			_format = new TextFormat();
			
			//文本
			_tf = new TextField();
			addChild(_tf);
			_tf.selectable = true;
			_tf.type = TextFieldType.INPUT;
			_tf.defaultTextFormat = _format;
			
			//文字更改事件
			_tf.addEventListener(Event.CHANGE, onChange);
			
			//描述文字
			if (_inputDes != null && _inputDes != "")
			{
				_tf.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			}
		}
		
		/////////////////////////////////////////////
		// 公开方法
		/////////////////////////////////////////////
		public override function draw():void
		{
			//文本格式
			_tf.defaultTextFormat = _format;
			
			//是否为数字输入
			if (_blNumber == true)
			{
				_tf.restrict = "0-9";
			}
			
			_tf.displayAsPassword = _password;
			
			_tf.text = (_text == "")? _inputDes:_text;
			
			//设置输入文本的宽度相对组件的宽度大小
			_tf.width = _width;
			
			//计算tf的高度
			if (_tf.text == "")
			{
				_tf.text = "X";
				_tf.height = Math.min(_tf.textHeight + 4, _height);
				_tf.text = "";
			}
			else
			{
				_tf.height = Math.min(_tf.textHeight + 4, _height);
			}
			
			//设置垂直居中
			_tf.y = Math.round(_height - _tf.height) * .5;
		}
		
		/////////////////////////////////////////////
		// 事件处理
		/////////////////////////////////////////////
		protected function onChange(e:Event):void
		{
			if (_blNumber)
			{
				var afterNum:int = int(_tf.text);
				if (afterNum > _max)
				{
					_tf.text = _text = _max.toString();
				}
				else if (afterNum < _min)
				{
					_tf.text = _text = _min.toString();
				}
				else
				{
					_text = _tf.text;
				}
			}
			else
			{
				_text = _tf.text;
			}
			
			e.stopImmediatePropagation();
			dispatchEvent(e);
		}
		
		/**
		 * 鼠标移入，设置介绍性文字
		 */		
		protected function onFocusIn(e:FocusEvent):void
		{
			IME.enabled = true;
			
			if (_tf.text == _inputDes)
			{
				_tf.text = "";
			}
			
			if (!_tf.hasEventListener(FocusEvent.FOCUS_OUT))
			{
				_tf.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			}
		}
		
		/**
		 * 鼠标移开
		 */		
		protected function onFocusOut(e:FocusEvent):void
		{
			_tf.removeEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			if (_tf.text == "")
			{
				_tf.text = _inputDes;
			}
		}
		
		/////////////////////////////////////////////
		// Getter & Setter
		/////////////////////////////////////////////
		public function set text(value:String):void
		{
			_text = (value == null)? "":value;
			
			invalidate();
		}
		public function get text():String
		{
			return _text;
		}
		
		/**
		 * 用于扩展访问TextField
		 */		
		public function get textField():TextField
		{
			return _tf;
		}
		
		/**
		 * 可输入文本的字符集(转发TextField.restrict)
		 */		
		public function set restrict(value:String):void
		{
			_tf.restrict = value;
		}
		public function get restrict():String
		{
			return _tf.restrict;
		}
		
		/**
		 * 最大输入字符个数
		 */		
		public function set maxChars(value:int):void
		{
			_tf.maxChars = value;
		}
		public function get maxChars():int
		{
			return _tf.maxChars;
		}
		
		/**
		 * 是否为密码格式文本
		 */		
		public function set password(value:Boolean):void
		{
			_password = value;
			invalidate();
		}
		public function get password():Boolean
		{
			return _password;
		}
		
		/**
		 * 是否为数字类型
		 */		
		public function set blNumber(value:Boolean):void
		{
			_blNumber = value;
			invalidate();
		}
		public function get blNumber():Boolean
		{
			return _blNumber;
		}
		
		/**
		 * 数字类型输入时的最大值
		 */		
		public function set max(value:int):void
		{
			_max = value;
			invalidate();
		}
		public function get max():int
		{
			return _max;
		}
		
		/**
		 * 数字类型输入时的最小值 
		 */		
		public function set min(value:int):void
		{
			_min = value;
			invalidate();
		}
		public function get min():int
		{
			return _min;
		}
		
		/**
		 * 设置文本对齐，有效值：TextFormatAlign.
		 */		
		public function set align(value:String):void
		{
			_format.align = value;
			invalidate();
		}
		
		/**
		 * 扩展enabled设置, 使文本始终可以相应tab
		 */		
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			_tf.tabEnabled = true;
		}
		
	}
}